<?php
!defined('IN_SYSTEM') && exit('Access Denied!');

class Category
{
    function GetCategory($cid=0)
	{
	    global $_G;
	   	return $_G['db']->fetch('category','*',array('cid' => $cid));
	}
	
    function GetCategoryByName($name)
	{
	    global $_G;
	    return $_G['db']->fetch('category','*',array('catename' => $name));
	}
    
    function GetSubCategory($pid=0,$type=0,$orderby="displayorder")
    {
    	global $_G;
    	return $_G['db']->fetch('category','*',array('pid' => $pid,'type'=>$type));
    }
    
	function GetCategoryList($pid=0,$type=0,$depth=0,$prefixchar='　')
	{
		$resultarray = array();
		$results = $this->GetSubCategory($pid,$type);
		if(count($results) > 0)
		{
			if($pid==0)
			{
				$depth = 0;
			}
			else
			{
				$depth++;
			}
			$prefix = str_repeat($prefixchar,$depth);
			for($i=0;$i<count($results);$i++)
			{
				$resultarray[] = $results[$i];
				$results[$i]->name = $prefix.$results[$i]->name;				
				
				$resultarray = array_merge($resultarray,$this->GetCategoryList($results[$i]->cid,$type,$depth));
			}
			
		}
		return $resultarray;
	}
}
?>